<?php namespace Admin\Controller; 

use Hdphp\Controller\Controller;

//商品控制器
class shopGoodsController extends CommonController{
	private $model;
	public function __init(){
	    parent::__init();
		//实例化shopGoods商品表
		$this->model = new \Common\Model\shopGoods;
	}
	
	//添加商品
	public function add(){
		//3.用户提交
		if(IS_POST){
			$_POST['gd_pic'] = implode(',', Q('post.gd_pic',array()));
			if(!$this->model->store()) View::error($this->model->getError());
			View::success('添加成功',U('index'));
		}
		//1.获得所属分类
		$cateModel = new \Common\Model\shopCate;
		$cateData = $cateModel->get();
		//把数据变成树枝状
		$cateData = Data::tree($cateData,'cname');
		View::with('cateData',$cateData);
		//2.获得所属品牌
		$brandModel = new \Common\Model\shopBrand;
		$brandData = $brandModel->get();
		View::with('brandData',$brandData);
		//默认模板
	    View::make();
	}
	
	//异步分类显示在桌面
	public function getTypeAttr(){
		//获取异步提交过来的tid(类型表的id)
		$tid = Q("post.tid",0,'intval');
		//实例化类型属性表
		$typeArrt = new \Common\Model\shopTypeArrt;
		//通过类型表的tid获得类型属性表的数据
		$typeData = $typeArrt->where("tid={$tid}")->get();
		//循环
		foreach ($typeData as $k => $v) {
			$typeData[$k]['tavalue'] = explode(',', $v['tavalue']);
		}
		echo json_encode($typeData);exit;
	}
	
	//商品列表
	public function index(){
		//获得所有数据
		$data = $this->model->get();
		View::with('data',$data);
		//默认模板
	    View::make();
	}
	
		
	//删除商品
	public function del(){
		//结束要删除数据的gid
		$gid = Q('get.gid',0,'intval');
		//删除对应数据
		$this->model->where("gid={$gid}")->delete();
		//成功提示
		View::success('删除成功',U('index'));
	}
		
	//编辑商品
	public function edit(){
		//2.用户提交
		if(IS_POST){
			$_POST['gd_pic'] = implode(',', Q('post.gd_pic',array()));
		    if(!$this->model->edit()) View::error($this->model->getError());
			View::success('编辑成功',U('index'));
		}
		//1.获得旧数据
		$gid = Q('get.gid',0,'intval');
		//(1)商品表旧数据(商品表和商品详细表关联就是把商品详细表的主键id可以放在隐藏域post提交)
		$oldData = $this->model->join('shop_goods_detail','detail_gid','=','gid')->join('shop_goods_attr','goods_gid','=','gid')->where("gid={$gid}")->find();
		View::with('oldData',$oldData);
		//(2)商品详情表旧数据
		$goodsDetailModel = new \Common\Model\shopGoodsDetail;
		$goodsDetailData = $goodsDetailModel->where("detail_gid={$gid}")->find();
		$goodsDetailData['gd_pic'] = explode(',', $goodsDetailData['gd_pic']);
		View::with('goodsDetailData',$goodsDetailData);
		//(3)商品属性表旧数据(商品属性表和类型属性表关联)
		$goodsAttrModel = new \Common\Model\shopGoodsAttr;
		$data = $goodsAttrModel->join('shop_type_arrt','type_taid','=','taid')->where("goods_gid={$gid}")->get();
		foreach ($data as $k => $v) {
			$data[$k]['tavalue'] = explode(',', $v['tavalue']);
		}
		View::with('data',$data);
		//编辑的时候一类里面只有第一个是增加,剩下的是删除
		$zjData = $goodsAttrModel->join('shop_type_arrt','type_taid','=','taid')->where("goods_gid={$gid} AND tatype=1")->groupBy('taname')->get();
		foreach ($zjData as $k => $v) {
			$gaid[] = $v['gaid'];
		}
		View::with('gaid',$gaid);
		//2.获得所属分类
		$cateModel = new \Common\Model\shopCate;
		$cateData = $cateModel->get();
		//把数据变成树枝状
		$cateData = Data::tree($cateData,'cname');
		View::with('cateData',$cateData);
		//3.获得所属品牌
		$brandModel = new \Common\Model\shopBrand;
		$brandData = $brandModel->get();
		View::with('brandData',$brandData);
		//载入模板
	    View::make();
	}	
		
    //列表图uploadify异步上传
	public function upload(){
	    $file = Upload::path('Upload/pic')->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}
	
	 //商品图册uploadify异步上传
	public function upload1(){
	    $file = Upload::path('Upload/gd_pic')->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
	